package com.lmqb.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lmqb.bean.SysMenu;
import com.lmqb.bean.SysRole;
import com.lmqb.bean.SysUser;
import com.lmqb.bean.SysUserOperationLog;
import com.lmqb.constant.MsgError;
import com.lmqb.constant.MsgSuccess;
import com.lmqb.entry.MenuTitle;
import com.lmqb.entry.RoleInfo;
import com.lmqb.entry.UpdateRole;
import com.lmqb.responces.ResultData;
import com.lmqb.service.JiaoYanService;
import com.lmqb.service.RoleService;
import com.lmqb.service.SysMenuService;
import com.lmqb.service.SysUserService;
import com.lmqb.util.StrUtil;

@Controller
public class RoleController extends BaseController{

	@Autowired
	private RoleService roleService;
	
	@Autowired
	private SysUserService sysUserService;
	
	@Autowired
	private SysMenuService sysMenuService;
	
	@Autowired
	private JiaoYanService jiaoYanService;
	
	//跳转到角色管理界面
	@RequestMapping(value="sysRole.html",method=RequestMethod.GET)
	public String toSysRole(HttpSession session,HttpServletRequest request,
			HttpServletResponse response){
		SysUser sysUser=(SysUser)session.getAttribute(memberAdmin);
		if(sysUser==null){
			return "login";
		}
		SysRole sysRole=new SysRole();
		List<SysRole> list=roleService.selectAll(sysRole);
		request.setAttribute(AllSysRoleF, list);
		MenuTitle menuTitle=new MenuTitle();
		menuTitle.setParent("系统配置");
		menuTitle.setMenu("角色管理");
		session.setAttribute(menuTitleF, menuTitle);
		return "sysRoleList";
	}
	
	//跳转到添加角色页面
	@RequestMapping(value="toAddRole.html",method=RequestMethod.GET)
	public String toAddRoles(HttpSession session,HttpServletRequest request,HttpServletResponse response){
		SysUser sysUser=(SysUser)session.getAttribute(memberAdmin);
		if(sysUser==null){
			return "login";
		}
		SysMenu sysMenu=new SysMenu();
		sysMenu.setMenuStatus((byte)1);
		List<SysMenu> list=sysMenuService.selectAll(sysMenu);
		request.setAttribute(AllSysMenuF, list);
		MenuTitle menuTitle=new MenuTitle();
		menuTitle.setParent("系统配置");
		menuTitle.setMenu("添加角色");
		session.setAttribute(menuTitleF, menuTitle);
		return "addRole";
	}
	
	//添加角色
	@RequestMapping(value="addRole",method=RequestMethod.POST)
	@ResponseBody
	public ResultData<String> addRole(HttpSession session,HttpServletRequest request,
			HttpServletResponse response,@RequestBody RoleInfo roleInfo){
		ResultData<String> resultData=new ResultData<String>();
		try{
			SysUser sysUser=(SysUser)session.getAttribute(memberAdmin);
			if(sysUser==null){
				resultData.setNotLogin();
				return resultData;
			}
			resultData=jiaoYanService.addRole(roleInfo);
			if(resultData.getStatus()==1){
				return resultData;
			}
			//添加角色。添加角色权限
			roleService.insertRole(roleInfo,sysUser);
			resultData.setSuccess(MsgSuccess.ChengGong, MsgSuccess.ChengGong);
		}catch(Exception e){
			e.printStackTrace();
			resultData.setError(MsgError.SysErr);
		}
		return resultData;
	}
	
	//停用角色
	@RequestMapping(value="delRole")
	@ResponseBody
	public ResultData<String> delRole(HttpSession session,HttpServletRequest request,
			HttpServletResponse response,Integer roleId){
		ResultData<String> resultData=new ResultData<String>();
		try{
			SysUser sysUser=(SysUser)session.getAttribute(memberAdmin);
			if(sysUser==null){
				resultData.setNotLogin();
				return resultData;
			}
			if(roleId==null){
				resultData.setError(MsgError.NullRoleId);
				return resultData;
			}
			SysRole sysRole1=roleService.selectByRoleId(roleId);
			SysRole sysRole=new SysRole();
			sysRole.setRoleId(roleId);
			sysRole.setRoleStatus((byte)2);
			roleService.update(sysRole);
			//添加管理员操作记录
			SysUserOperationLog log=new SysUserOperationLog();
			log.setAddTime(StrUtil.timeToInt(new Date()));
			log.setLoginName(sysUser.getLoginName());
			log.setObjectId(roleId+"");
			log.setOperationCate((byte)3);
			log.setOperationContent("停用角色"+sysRole1.getRoleName()+",角色id："+roleId);
			log.setRealName(sysUser.getRealName());
			log.setUserId(sysUser.getUserId());
			log.setTableName("sys_role");
			sysUserService.insertLog(log);
			resultData.setSuccess(MsgSuccess.ChengGong, MsgSuccess.ChengGong);
		}catch(Exception e){
			e.printStackTrace();
			resultData.setError(MsgError.SysErr);
		}
		return resultData;
	}
	
	//恢复角色
	@RequestMapping(value="recoveryRole")
	@ResponseBody
	public ResultData<String> recoveryRole(HttpSession session,HttpServletRequest request,
			HttpServletResponse response,Integer roleId){
		ResultData<String> resultData=new ResultData<String>();
		try{
			SysUser sysUser=(SysUser)session.getAttribute(memberAdmin);
			if(sysUser==null){
				resultData.setNotLogin();
				return resultData;
			}
			if(roleId==null){
				resultData.setError(MsgError.NullRoleId);
				return resultData;
			}
			SysRole sysRole1=roleService.selectByRoleId(roleId);
			SysRole sysRole=new SysRole();
			sysRole.setRoleId(roleId);
			sysRole.setRoleStatus((byte)1);
			roleService.update(sysRole);
			//添加管理员操作记录
			SysUserOperationLog log=new SysUserOperationLog();
			log.setAddTime(StrUtil.timeToInt(new Date()));
			log.setLoginName(sysUser.getLoginName());
			log.setObjectId(roleId+"");
			log.setOperationCate((byte)3);
			log.setOperationContent("恢复角色"+sysRole1.getRoleName()+",角色id："+roleId);
			log.setRealName(sysUser.getRealName());
			log.setUserId(sysUser.getUserId());
			log.setTableName("sys_role");
			sysUserService.insertLog(log);
			resultData.setSuccess(MsgSuccess.ChengGong, MsgSuccess.ChengGong);
		}catch(Exception e){
			e.printStackTrace();
			resultData.setError(MsgError.SysErr);
		}
		return resultData;
	}
	
	//跳转到修改角色页面
	@RequestMapping(value="toModifyRole.html",method=RequestMethod.GET)
	public String toModifyRole(HttpSession session,HttpServletRequest request,
			HttpServletResponse response,Integer roleId){
		SysUser sysUser=(SysUser)session.getAttribute(memberAdmin);
		if(sysUser==null){
			return "login";
		}
		UpdateRole updateRole=roleService.selectById(roleId);
		request.setAttribute(updateRoleF, updateRole);
		MenuTitle menuTitle=new MenuTitle();
		menuTitle.setParent("系统配置");
		menuTitle.setMenu("修改角色");
		session.setAttribute(menuTitleF, menuTitle);
		return "modifyRole";
	}
	
	//修改角色
	@RequestMapping(value="modifyRole",method=RequestMethod.POST)
	@ResponseBody
	public ResultData<String> modifyRole(HttpSession session,HttpServletRequest request,
			HttpServletResponse response,@RequestBody RoleInfo roleInfo){
		ResultData<String> resultData=new ResultData<String>();
		try{
			SysUser sysUser=(SysUser)session.getAttribute(memberAdmin);
			if(sysUser==null){
				resultData.setNotLogin();
				return resultData;
			}
			resultData=jiaoYanService.modifyRole(roleInfo);
			if(resultData.getStatus()==1){
				return resultData;
			}
			//修改角色。修改角色权限（删除原来的，增加新的）
			roleService.modifyRole(roleInfo,sysUser);
			resultData.setSuccess(MsgSuccess.ChengGong, MsgSuccess.ChengGong);
		}catch(Exception e){
			e.printStackTrace();
			resultData.setError(MsgError.SysErr);
		}
		return resultData;
	}
}
